#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <linux/spi/spi.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

static int major = 0;
static struct class *chip_class;

static int chip_open(struct inode *inode, struct file *file)
{
	return 0;
}

static ssize_t chip_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	return 0;
}

static int chip_release(struct inode *inode, struct file *filp)
{
 	return 0;
}

static struct file_operations chip_ops = {
    .owner  = THIS_MODULE,
    .open   = chip_open,
    .read   = chip_read,
    .release = chip_release,
};

static int chip_spi_probe(struct spi_device *spi)
{
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    ap3216c_client = client;

    /* register_chrdev */
    major = register_chrdev(0,"chip",&chip_ops);

    chip_class = class_create(THIS_MODULE,"chip_class");
    device_create(chip_class, NULL, MKDEV(major, 0), NULL, "chip"); 

	/* init spi_device */
 	spi->mode = SPI_MODE_0; /*MODE0，CPOL=0，CPHA=0*/
 	spi_setup(spi);
	
    return 0; 

}

static int chip_spi_remove(struct platform_device *pdev)
{
	struct spi_master *master = platform_get_drvdata(pdev);
	
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	
    /* 顺序与probe函数相反 */
    device_destroy(chip_class, MKDEV(major, 0));
    class_destroy(chip_class);

    /* unregister_chrdev */
    unregister_chrdev(major,"chip");

	return 0;
}

static const struct of_device_id chip_of_match[] = {
	{ .compatible = "huahui,chip", },
	{ }
};

static const struct spi_device_id chip_id[] = {
	{"chip",	0},
	{}
};


static struct spi_driver chip_spi_driver = {
	.driver = {
		.name	= "chip",
		.of_match_table = of_match_ptr(chip_of_match),
	},
	.probe		= chip_spi_probe,
	.remove		= chip_spi_remove,
	.id_table	= chip_id,
};

module_spi_driver(chip_spi_driver);

MODULE_DESCRIPTION("chip SPI Controller driver");
MODULE_AUTHOR("huahui");
MODULE_LICENSE("GPL");
